package com.asjy.exception;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.security.spec.ECField;

/**
 * @Auther: lilinlin
 * @Date: 2021/4/20
 * @Description: com.asjy.exception
 * @version: 1.0
 */
@Component
public class ExceptionResovler implements HandlerExceptionResolver {
    //异常处理的方法
    //ex:异常对象
    //返回值：ModelAndView
    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
         Logger log = Logger.getLogger(ExceptionResovler.class);
        log.error("出错地址:"+request.getRequestURL());
        MyException myException = null;
        //判断ex是否是MyException异常类型
        if(ex instanceof  MyException){
            myException = (MyException)ex;
        }else{
            myException = new MyException("系统正在维护中...");
        }
        ModelAndView mv = new ModelAndView();
        mv.addObject("msg",myException.getMessage());
        mv.setViewName("error");//视图解析器
        return mv;
    }
}